Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs.
Through leveraging generators Koa allows you to ditch callbacks and greatly increase error-handling. Koa does not bundle any middleware within core, and provides an elegant suite of methods that make writing servers fast and enjoyable.
Let’s create a simple Koa application, and review some useful middlewares for it.
Requirements
Koa requires Node 7.6.0
or higher for async/await
syntax.
You can use older Node versions with Babel (see more about it at Koa installation section).
Environment
Node.js
If Node is not installed yet, you can download it here. If you are a lucky owner of Linux or macOS, package managers can be used - this method described here.
To check that the Node installation is correct, you can run the command:
1 | $ node --version |
That will print the Node version.
Also for NPM:
Also don’t forget about NPM (Node Package Manager):
1 | $ npm --version |
You can use NVM (Node Version Manager) and if you know what it is, it shouldn’t be a problem.
Dependencies installation
Create directory where all the code will be located. Run terminal and open this directory.
Let’s initialize the package environment. This, at a minimum, will help us keep the list of dependencies with our code, and as a maximum - create a great package for distribution to the community.
1 | $ npm init |
NPM will ask you a few questions. Type answers for package name of author.
1 | $ npm init |
You should see the package.json
file that NPM has created.
Install koa
:
1 | $ npm i koa |
Hello, World!
Let’s create a file index.js
, and copy-paste the next slightly modified version of standard Koa example.
1 | const Koa = require('koa'); |
Code is simple to understand.
We’ve created a Koa application instance. With a use
method you can add a middleware to the chain. All middlewares will be executed in sequence with each request. Each middleware accepts two arguments - ctx
and next
, where ctx
is a request context (obviously), and next
- the next middleware in the chain. In the last middleware the next
argument can be omitted (as in example).
After that we’ve tell Koa to listen the port 3000
. Callback allows us to understand that the server actually started, rather than hangs.
Now we can start the server:
1 | $ node index.js |
If the server has successfully started, open the browser and go to localhost:3000
. You should see the Hello, World!
page.
You can read more about Koa functionality on the Koa website.
Useful stuff
You can find alternatives for every middleware in the next list, as well as other middlewares.
Koa-router
The library allows you to get the necessary functionality of routing requests to different handlers. Without this middleware, it’s not very possible to do something more than Hello, World!
.
In addition to koa-router
, there are a lot of other analogues, for example, a simpler [koa-route
] (https://github.com/koajs/route), which is difficult to come up with any application for ambitious purposes, and a router with data validation of incoming and outgoing data in requests/reponses [joi-router
] (https://github.com/koajs/joi-router). Nevertheless, koa-router
is the most popular at the moment, and it has balanced functionality, nothing superfluous.
1 | $ npm i koa-router |
1 | const Koa = require('koa'); |
Read more about koa-router
here.
Koa-bodyparser
Koa out of the box doesn’t know how to parse some data in requests.
If we need to accept requests with a JSON body, for example, then this middleware is necessary.
1 | $ npm i koa-bodyparser |
1 | const Koa = require('koa'); |
More about koa-bodyparser
is here.
Koa-logger
Logs of what requested, and what responded, are also needed. For development, the koa-logger
can be used.
It will look something like this (taken from the page of the package):
1 | <-- GET / |
Very simple. If you want more information about requests, you can try [bunyan-logger
] (https://github.com/koajs/bunyan-logger).
1 | $ npm i koa-logger |
1 | const Koa = require('koa'); |
Repository of koa-logger
is located here.
@koa/cors
CORS requests sometimes are needed.
1 | $ npm i @koa/cors@2 |
1 | const Koa = require('koa'); |
Repository of @koa/cors
is here.
Koa-compress
Compress responses from the server.
1 | $ npm i koa-compress |
1 | const Koa = require('koa'); |
Read more about koa-compress
here.
Koa-session
If cookie sessions are required, then this is a good package.
1 | $ npm i koa-session |
1 | const Koa = require('koa'); |
More koa-session
options is here.
Koa-JWT
And JWT, of course. Very useful for API development. It’s a little difficult to come up with a simple example using this library, so you can read README in the package repository.
1 | $ npm i koa-jwt |
Read more about koa-jwt
here.
In conclusion
Koa performs its only important function - to accept requests to the server, and apply a middleware chain to them. This allows you to achieve good flexibility in the development of the application, and to use only what is needed.
It’s also very similar to [Express
] (https://expressjs.com/).
So there should not be any problems with it.
Useful links
- koa (koajs.com)
- Koa examples
- Koa wiki - links to the other useful stuff for Koa.
- Awesome Node.js - awesome list for Node.js.
- Awesome Koa - awesome list for Koa.